//
//  ReportMainViewController.m
//  MirrorCharge
//
//  Created by dllo on 16/10/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ReportMainViewController.h"
#import "PNChart.h"
#import "Masonry.h"
#import "MJReportMain.h"
#import "LocalJSONAnalytical.h"
#import "YYModel.h"
#import "UIColor+HexRGB.h"
@interface ReportMainViewController ()

@property (weak, nonatomic) IBOutlet UISegmentedControl *mj_segmentControl;

@property (weak, nonatomic) IBOutlet UIView *rightView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthLayout;
@property(nonatomic, strong) NSMutableArray <PNPieChartDataItem *> *items;

@end

@implementation ReportMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fileName = nil;
}
- (void)setFileName:(NSString *)fileName {
    _fileName = fileName;
    [self handleData];
    [self setupSubviews];
}
- (void)handleData {
    if (_fileName == nil) {
        _fileName = @"MJReportMain";
    }
    _items = [NSMutableArray array];
    NSDictionary *result = [LocalJSONAnalytical JSONAnalyticalWithFileName:_fileName];
    for (NSDictionary *dic in result[@"data"]) {
        MJReportMain *model = [MJReportMain yy_modelWithDictionary:dic];
        PNPieChartDataItem *item = [PNPieChartDataItem dataItemWithValue:model.pay * 100 / model.total color:[UIColor colorFromHexRGB:model.color] description:model.category];
        [_items addObject:item];
        
    }
    NSLog(@"%@",_dataArray);
}

- (void)setupSubviews {
    self.view.backgroundColor = [UIColor grayColor];
    self.mj_segmentControl.layer.cornerRadius = 10;
    self.mj_segmentControl.layer.masksToBounds = YES;
    self.mj_segmentControl.layer.borderWidth = 1;
    self.mj_segmentControl.layer.borderColor = [UIColor redColor].CGColor;
    [self.mj_segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    // 饼状图
    
    PNPieChart *pieChart = [[PNPieChart alloc] initWithFrame:CGRectZero items:_items];
    [self.view addSubview:pieChart];
    [pieChart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mj_segmentControl.mas_bottom).mas_offset(100);
        make.left.equalTo(self.view).mas_offset(10);
        make.right.equalTo(self.view).mas_offset(-120);
        make.height.equalTo(pieChart.mas_width);
        
    }];
    pieChart.descriptionTextFont = [UIFont systemFontOfSize:20];
    [pieChart strokeChart];
    // 抽屉
    [self.view addSubview:_rightView];
    //把左侧边的view先隐藏
    UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.view addGestureRecognizer:pan];
}
- (void)segmentAction:(UISegmentedControl *)segment {
    if (segment.selectedSegmentIndex == 0) {
        [self closeView:NO];
    }else {
        [self closeView:YES];
    }
}

- (void)handlePan:(UIPanGestureRecognizer*) recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    //增量后的x坐标位置
    CGFloat Xresult = translation.x;
    if (Xresult < 0) {
        _widthLayout.constant = -Xresult + 100;
    } else {
        _widthLayout.constant = -Xresult + SCREEN_WIDTH;
    }
    //做弹回效果，以中轴为界限
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        
        if (_widthLayout.constant > SCREEN_WIDTH / 2) {
            [self closeView:YES];
        } else {
            [self closeView:NO];
        }
    }
}

- (void)closeView:(BOOL)close {
    
    if (close) {
        _widthLayout.constant = SCREEN_WIDTH;
    } else {
        _widthLayout.constant = 100;
    }
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (_widthLayout.constant == 100) {
            _mj_segmentControl.selectedSegmentIndex = 0;
        } else {
            _mj_segmentControl.selectedSegmentIndex = 1;
        }
        
       
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end
